package com.humanminoritygame.model;

import java.util.SortedSet;
import java.util.TreeSet;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class HibernateCategoryRepo extends HibernateGenericRepo implements CategoryRepo{

  @Autowired
  public HibernateCategoryRepo(SessionFactory sessionFactory) {
    setSessionFactory(sessionFactory);
  }

  @Override
  public void addCategory(Category category) {
    Session session = getSession();
    session.save(category);
    session.flush();
  }

  @SuppressWarnings("unchecked")
  @Override
  public SortedSet<Category> getAllCategories() {
    SortedSet<Category> categories = new TreeSet<Category>();
    categories.addAll(getSession().createQuery("SELECT c FROM Category c ORDER BY c.frequency").list());
    return categories;
  }

  @Override
  public Category getCategoryById(Long id) {
    return (Category)getSession().get(Category.class, id);
  }

}
